#define _CRT_SECURE_NO_WARNINGS

#include "paramfile.h"
#include <stdlib.h>
#include <stdio.h>

int getparams1(char* filename, TInpPar1* ppar)
{
FILE *file = fopen ( filename, "r" );

if ( file != NULL )
{
	int i;
	char line [ 256 ]; /* or other suitable maximum line size */
	fgets ( line, sizeof line, file );
	sscanf(line,"dim=%d",&ppar->dim);
	    
	fgets ( line, sizeof line, file );
	sscanf(line,"step=%lf",&ppar->step);

	fgets ( line, sizeof line, file );
	sscanf(line,"d=%lf",&ppar->d);

	fgets ( line, sizeof line, file );
	sscanf(line,"a0=%lf",&ppar->a0);

	fgets ( line, sizeof line, file );
	sscanf(line,"force=%d",&ppar->force);

	fgets ( line, sizeof line, file );
	sscanf(line,"initfile=%s",&ppar->initfile);

	fgets ( line, sizeof line, file );
	sscanf(line,"initraw=%d",&ppar->initraw);

	fgets ( line, sizeof line, file );
	sscanf(line,"save=%d",&ppar->savemnum);
	
	ppar->savemoments = (int*) malloc (sizeof(int)*ppar->savemnum);
	for(i = 0; i<ppar->savemnum;i++)
	{
		fgets ( line, sizeof line, file );	
		sscanf(line,"%d",&ppar->savemoments[i]);
	}
	fclose ( file );
	return 0;
}
else 
	return 1;
}

int getinitvals(char* filename, double* xinit, int dim, int raw)
{
FILE *file = fopen ( filename, "r" );

if ( file != NULL )
{
	int i;
	char line [ 256 ]; 
	for(i = 0; i<dim;i++)
	{
		if (fgets ( line, sizeof line, file) ==0)	
			return 1;
		if (raw == 1)
			sscanf(line,"%lf",&xinit[i]);	
		else
			sscanf(line,"%*s %lf",&xinit[i]);
	}
	fclose ( file );
	return 0;
}
else 
	return 1;
}


int freeparams1(TInpPar1* ppar)
{
	free(ppar->savemoments);
	return 0;
}

int getparams2(char* filename, TInpPar2* ppar)
{
FILE *file = fopen ( filename, "r" );

if ( file != NULL )
{
	int i;
	char line [ 256 ]; /* or other suitable maximum line size */
	fgets ( line, sizeof line, file );
	sscanf(line,"dim=%d",&ppar->dim);
	    
	fgets ( line, sizeof line, file );
	sscanf(line,"step=%lf",&ppar->step);

	fgets ( line, sizeof line, file );
	sscanf(line,"d=%lf",&ppar->d);

	fgets ( line, sizeof line, file );
	sscanf(line,"a0=%lf",&ppar->a0);

	fgets ( line, sizeof line, file );
	sscanf(line,"force=%d",&ppar->force);

	fgets ( line, sizeof line, file );
	sscanf(line,"initfile=%s",&ppar->initfile);

	fgets ( line, sizeof line, file );
	sscanf(line,"initraw=%d",&ppar->initraw);

	fgets ( line, sizeof line, file );
	sscanf(line,"save=%d",&ppar->savemnum);
	
	ppar->savemoments = (int*) malloc (sizeof(int)*ppar->savemnum);
	for(i = 0; i<ppar->savemnum;i++)
	{
		fgets ( line, sizeof line, file );	
		sscanf(line,"%d",&ppar->savemoments[i]);
	}
	fclose ( file );
	return 0;
}
else 
	return 1;
}

int freeparams2(TInpPar2* ppar)
{
	free(ppar->savemoments);
	return 0;
}

